package com.zego.wawaji.ui.wawa;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.noble.library.basic.Constant;
import com.noble.library.basic.log.PLogger;
import com.noble.library.basic.sp.PSP;
import com.noble.library.basic.string.StringUtils;
import com.noble.library.module.ModuleBase;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.bean.Common;
import com.zego.wawaji.base.bean.Updata;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.base.utils.InputUtils;
import com.zego.wawaji.base.utils.NetworkUtils;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.message.RecMessageMgr;
import com.zego.wawaji.third.pay.bean.Alipay;
import com.zego.wawaji.third.pay.bean.PayWX;
import com.zego.wawaji.third.pay.util.PayAlipayUtils;
import com.zego.wawaji.third.pay.util.PayWeixinUtils;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.bean.pay.PayList;
import com.zego.wawaji.ui.bean.wawa.Game.Game;
import com.zego.wawaji.ui.bean.wawa.Wawa;
import com.zego.wawaji.ui.bean.wawa.WawaConstant;
import com.zego.wawaji.ui.bean.wawa.WawaJi;
import com.zego.wawaji.ui.bean.wawa.WawaList;
import com.zego.wawaji.ui.center.transport.bean.StepList;
import com.zego.wawaji.ui.dialog.LoadingDialog;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.play.bean.PlayInit;
import com.zego.wawaji.ui.view.adDialog.AdConstant;
import com.zego.wawaji.ui.view.adDialog.AdManager;
import com.zego.wawaji.ui.view.adDialog.bean.AdInfo;
import com.zego.wawaji.ui.view.adDialog.bean.AdInfoList;
import com.zego.wawaji.ui.view.adDialog.transformer.DepthPageTransformer;
import com.zego.wawaji.wawaji_client.AppTinkerLike;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 娃娃相关管理类
 * Created by Su on 2017/11/28.
 */
public class WawaMgr implements ModuleBase {
    private static final String WAWA_LIST = "wawa_list";  // 娃娃种类列表
    private static WawaList wawaList = null;
    private RecMessageMgr messageMgr = new RecMessageMgr();

    @Override
    public void init() {
        messageMgr.init();
    }

    @Override
    public void release() {
    }

    // ========================================== 常量保存 ===============================
    private static WawaJi lastWawaJi;  // 保存用户最近一次进入的房间信息

    public void setLastWawaJi(WawaJi wawaJi) {
        lastWawaJi = wawaJi;
    }

    public WawaJi getLastWawaJi() {
        return lastWawaJi;
    }


    // ========================================== 方法 ===============================

    /**
     * 获取娃娃种类列表
     */
    public WawaList getWawaList() {
        if (wawaList == null || wawaList.getWawaList().size() <= 0) {
            wawaList = new WawaList();
            String result = PSP.getInstance().getString(WAWA_LIST, "");
            if (!TextUtils.isEmpty(result)) {
                wawaList.parseJson(result);
            }
        }
        return wawaList;
    }

    /**
     * 根据娃娃id获取娃娃对象
     */
    public Wawa getWawa(int id) {
        if (!getWawaList().getWawaList().isEmpty()) {
            for (Wawa wawa : getWawaList().getWawaList()) {
                if (wawa.getId() == id) return wawa;
            }
        }
        return new Wawa();
    }

    /**
     * 获取合成娃娃碎片列表
     */
    public List<Wawa> getMaterialList() {
        List<Wawa> marerialList = new ArrayList<>();
        if (!getWawaList().getWawaList().isEmpty()) {
            marerialList.clear();
            for (Wawa wawa : getWawaList().getWawaList()) {
                if (wawa.isMaterial()) {
                    marerialList.add(wawa);
                }
            }
        }
        return marerialList;
    }

    /**
     * 获取娃娃列表
     */
    public void reqWawaList(final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqPostAndCacheHttp(UrlParam.reqWawaList, new HashMap<>(), response -> {
            if (response.isOk()) {
                wawaList = (WawaList) response.getBaseData();
                saveWawaList(response.getResponseString());
            }
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    /**
     * 保存到SP
     */
    private void saveWawaList(String resultStr) {
        PSP.getInstance().put(WAWA_LIST, resultStr);
    }

    /**
     * 获取娃娃机列表
     */
    public void reqWawajiList(final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("limit", 50);
        postParam.put("sortby", "WwjStatus,Id");
        postParam.put("order", "desc,asc");
        ModuleMgr.getHttpMgr().reqPostAndCacheHttp(UrlParam.reqWawajiList, postParam, complete);
    }

    private static PayList payList = new PayList();           // 商品列表

    /**
     * 获取充值列表
     */
    public PayList getPayList() {
        if (payList == null) {
            reqPayListNew(null);
        }
        return payList;
    }

    public void reqPayListNew(final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqPostAndCacheHttp(UrlParam.reqPayListNew, new HashMap<>(), response -> {
            if (complete != null)
                complete.onRequestComplete(response);

            if (response.isOk()) {
                payList = (PayList) response.getBaseData();
            }
        });
    }

    public static int payType = 3;  // 可用支付PayStatus 1：微信 2：支付宝 3：两者都可用

    /**
     * 获取可用支付方式
     * <p>
     * app_id: 1: 原包  2： 微信支付包   3： 换皮包
     */
    public void reqPayType() {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("app_id", Constant.getAppId());
        ModuleMgr.getHttpMgr().reqGetAndCacheHttp(UrlParam.reqPayType, getParam, response -> {
            if (response.isOk()) {
                JSONObject jsonObject = response.getResponseObject();
                payType = jsonObject.optInt("PayStatus");
            }
        });
    }

    /**
     * 获取我的娃娃列表
     *
     * @param status 需查找的列表类型 {@see 'WawaConstant'}
     * @param page   页码，从1开始
     */
    public void reqMyWawa(int status, int page, RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("status", status);
        postParam.put("limit", 1000);
        postParam.put("offset", 10 * (page - 1)); // 查找偏移量
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqMyWawa, postParam, complete);
    }

    /**
     * 获取已申请发货娃娃列表
     *
     * @param page 页码，从1开始
     */
    public void reqAskSend(int page, RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("limit", 10);
        postParam.put("offset", 10 * (page - 1)); // 查找偏移量
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqAskSend, postParam, complete);
    }

    /**
     * 获取充值列表
     *
     * @param page 页码，从1开始
     */
    public void reqOrderRecord(int page, RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("limit", 10);
        postParam.put("offset", 10 * (page - 1)); // 查找偏移量
        ModuleMgr.getHttpMgr().reqPostAndCacheHttp(UrlParam.reqOrderRecord, postParam, complete);
    }

    /**
     * 获取消费记录
     *
     * @param page 页码，从1开始
     */
    public void reqSpendRecord(int page, RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("limit", 10);
        postParam.put("offset", 10 * (page - 1)); // 查找偏移量
        ModuleMgr.getHttpMgr().reqPostAndCacheHttp(UrlParam.reqSpendRecord, postParam, complete);
    }

    /**
     * 获取积分记录
     *
     * @param page 页码，从1开始
     */
    public void reqPointRecord(int page, RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("limit", 10);
        postParam.put("offset", 10 * (page - 1)); // 查找偏移量
        ModuleMgr.getHttpMgr().reqPostAndCacheHttp(UrlParam.reqPointRecord, postParam, complete);
    }

    /**
     * 选择娃娃发货
     */
    public void reqSendChooseWawa(String ids, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "发货申请中...");
        ModuleMgr.getHttpMgr().reqJsonNoCacheHttp(UrlParam.reqSendChooseWawa, ids, response -> {
            LoadingDialog.closeLoadingDialog(200);
            if (complete != null) {
                complete.onRequestComplete(response);
            }
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
            }
        });
    }

    /**
     * 我的娃娃：兑换娃娃币/积分
     */
    public void reqExchangeWawa(long id, final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("game_id", id);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqExchangeWawa, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);

            if (!response.isOk())
                PToast.showShort(response.getMsg());
        });
    }

    private static long gameId;     // 游戏id
    public static PlayInit playInit;     // 游戏配置

    /**
     * 游戏预约通知服务器
     */
    public void reqGameApply(long rid, int wwTypeId, final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("ww_type_id", wwTypeId);
        postParam.put("rid", rid);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqGameApply, postParam, response -> {
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
            } else {
                playInit = (PlayInit) response.getBaseData();
            }
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    /**
     * 加入游戏房间，通知服务器
     */
    public void reqRoomjoin(long rid, final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("rid", rid);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqRoomjoin, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    /**
     * 开始游戏时通知服务端
     *
     * @param wwTypeId 娃娃id
     */
    public void reqGameAdd(long roomId, int wwTypeId, final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("Rid", roomId);
        postParam.put("WwTypeId", wwTypeId);
        ModuleMgr.getHttpMgr().reqJsonNoCacheHttp(UrlParam.reqGameAdd, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
                return;
            }
            Game game = (Game) response.getBaseData();
            gameId = game.getGameId();
            MsgMgr.getInstance().sendMsg(MsgType.MT_Confirm_Board, null); // 通知手动扣费
        });
    }

    public void reqGetGameResult(RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("id_game", gameId);
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqGetGameResult, postParam, complete);
    }

    public long getGameId() {
        return gameId;
    }

    /**
     * 结束游戏时通知服务端
     *
     * @param take 1: 成功抓到  -1：没有抓到
     */
    public void reqGameOver(final int take, final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("game_id", gameId);
        postParam.put("take", take);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqGameOver, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);

            if (take == 1) { // 抓中
                MsgMgr.getInstance().sendMsg(MsgType.MT_Wawa_Untreated, null);
            }
        });
    }

    /**
     * 获取支付宝充值订单
     */
    public void reqAlipay(String subject, int productCode, final RequestComplete complete) {
        reqAlipay(subject, productCode, -1, complete);
    }

    public void reqAlipay(String subject, int productCode, long couponId, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "正在获取订单信息");
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("subject", subject);
        postParam.put("product_id", productCode);
        postParam.put("rec_coupon_id", couponId);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqAlipay, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);

            LoadingDialog.closeLoadingDialog();
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
                return;
            }
            Alipay alipay = (Alipay) response.getBaseData();
            new PayAlipayUtils(AppTinkerLike.getActivity()).pay(alipay.getUrl());
        });
    }

    /**
     * 获取微信充值订单
     */
    public void reqWXPay(String subject, int productCode, final RequestComplete complete) {
        reqWXPay(subject, productCode, -1, complete);
    }

    public void reqWXPay(String subject, int productCode, long couponId, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "正在获取订单信息");
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("app_id", Constant.getAppId());
        postParam.put("subject", subject);
        postParam.put("product_id", productCode);
        postParam.put("rec_coupon_id", couponId);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqWxPay, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);

            LoadingDialog.closeLoadingDialog();
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
                return;
            }
            PayWX payWX = (PayWX) response.getBaseData();
            new PayWeixinUtils(AppTinkerLike.getActivity()).pay(payWX);
        });
    }

    /**
     * 检查更新
     *
     * @param adInfo 是否展示广告
     */
    public void reqNewVersion(final boolean show, final boolean adInfo, final RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("app_id", Constant.getAppId());
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqNewVersion, getParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);
            LoadingDialog.closeLoadingDialog(200);
            if (response.isOk()) {
                Updata updata = (Updata) response.getBaseData();
                if (updata == null) return;
                if (updata.getVerCode() > ModuleMgr.getAppMgr().getVerCode()) { // 发现新版本
                    PickerDialogUtil.showUpdateDialog((FragmentActivity) AppTinkerLike.getActivity(), updata);
                    return;
                }
            }
            if (show) {
                PToast.showShort("您当前为最新版本");
                return;
            }

            // 获取是否有推送广告
            if (adInfo)
                reqAdInfo();
        });
    }

    public void reqNewVersion(final boolean show, final RequestComplete complete) {
        reqNewVersion(show, true, complete);
    }

    /**
     * 获取轮播Banner公告
     */
    public void reqBannerNotice(final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("app_id", 1);
        ModuleMgr.getHttpMgr().reqPostAndCacheHttp(UrlParam.reqBannerNotice, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    /**
     * 获取邀请信息
     */
    public void reqShareInfo(final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqShareInfo, new HashMap<>(), response -> {
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    /**
     * 积分兑换娃娃币
     */
    public void reqExchangeWwcoin(int point, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "兑换中...");
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("point", point);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqExchangeWwcoin, postParam, response -> {
            LoadingDialog.closeLoadingDialog();
            if (complete != null)
                complete.onRequestComplete(response);

            if (response.isOk()) {
                MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
            } else {
                PToast.showShort(response.getMsg());
            }
        });
    }

    /**
     * 获取物流信息
     *
     * @param tid 内部物流ID
     */
    public void reqTransport(final long tid, final RequestComplete complete) {
        if (tid <= 0) {
            PToast.showShort("无法获取物流信息，请耐心等待");
            return;
        }
        LoadingDialog.show(AppTinkerLike.getActivity(), "数据获取中...");
        final Map<String, Object> postParam = new HashMap<>();
        postParam.put("tid", tid);
        ModuleMgr.getHttpMgr().reqPostAndCacheHttp(UrlParam.reqTransportID, postParam, response -> {
            String transportId = "";
            if (response.isOk()) {
                JSONObject jsonObject = response.getResponseObject();
                transportId = jsonObject.optString("TranTag");
            }
            reqTransportInfo(postParam, transportId, complete);
        });
    }

    /**
     * 获取物流信息
     */
    private void reqTransportInfo(Map<String, Object> postParam, final String transportId, final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqTransport, postParam, response -> {
            LoadingDialog.closeLoadingDialog();
            if (complete != null)
                complete.onRequestComplete(response);

            if (!response.isOk()) {
                if (TextUtils.isEmpty(transportId)) {
                    PToast.showShort(response.getMsg());
                } else {
                    UIShow.showWebActivity(AppTinkerLike.getActivity(), 1, "https://www.baidu.com/s?wd=" + transportId);
                }
                return;
            }
            StepList stepList = (StepList) response.getBaseData();
            UIShow.showWaWaLogisticsAct(AppTinkerLike.getActivity(), stepList);
        });
    }

    /**
     * 获取最近抓中记录
     */
    public void reqGameCatch(long rid, final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("rid", rid);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqGameCatch, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    /**
     * 分享朋友圈成功奖励，每日一次
     *
     * @param share_target 1: 朋友圈  2： 好友
     */
    public void reqShareWXCircle(int share_target, final RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("share_target", share_target);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqShareWXCircle, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);
            if (response.isOk()) {
                ModuleMgr.getCenterMgr().reqMyInfo(null);
            }
        });
    }

    /**
     * 邀请Ip地址通知服务器
     */
    public void reqYQIp() {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("pip", NetworkUtils.ipLine);   // 用户公网IP
        postParam.put("iip", NetworkUtils.getIpAddressString()); // 获取内网IP
        postParam.put("yq_code", InputUtils.getTextFromClip()); // 获取剪切板邀请码
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqYQIp, postParam, null);
    }

    /**
     * 输入邀请码通知服务器
     */
    public void reqYQCode(String yqCode) {
        if (TextUtils.isEmpty(yqCode)) {    // 无邀请码
            return;
        }

        Map<String, Object> postParam = new HashMap<>();
        postParam.put("yq_code", yqCode); // 获取剪切板邀请码
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqYQCode, postParam, response -> {
            String res = response.getResString();
            if (response.isOk()) {
                try {
                    if (TextUtils.isEmpty(res)) return;
                    JSONObject jsonObject = new JSONObject(res);
                    int awardNum = jsonObject.optInt("InviteWwCoinZsYqNew");
                    if (awardNum > 0) {
                        PickerDialogUtil.showInviteDialog((FragmentActivity) AppTinkerLike.getActivity(), awardNum);
                        return;
                    }
                    //PToast.showShort("请求出错");
                } catch (Exception e) {
                    //PToast.showShort("请求出错");
                }
            } else {
                PToast.showShort(response.getMsg());
            }

            // 未正确拿到邀请奖励， 执行版本更新
            reqNewVersion(false, null);
        });
    }

    /**
     * 获取关于我信息
     */
    public void reqAboutMe(final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqGetAndCacheHttp(UrlParam.reqAboutMe, null, response -> {
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    private static Common common;

    public Common getCommon() {
        if (common == null) {
            return new Common();
        }
        return common;
    }

    /**
     * 获取基本数据
     */
    public void reqSetting(RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("rank", 2);
        ModuleMgr.getHttpMgr().reqGetAndCacheHttp(UrlParam.reqSetting, getParam, response -> {
            if (response.isOk()) {
                common = (Common) response.getBaseData();
            }
        });
    }

    // ====================================== 广告推送弹框 ===================================
    private AdInfoList adInfoList;

    public AdInfoList getAdInfoList() {
        return adInfoList;
    }

    /**
     * 获取广告弹窗数据
     */
    private void reqAdInfo() {
//        if (!IntervalUtil.checkAdDate()) return;

        Map<String, Object> getParam = new HashMap<>();
        getParam.put("app_id", "1");
        getParam.put("ver", ModuleMgr.getAppMgr().getVerCode());
        getParam.put("os", "android");
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqAdInfo, getParam, response -> {
            if (!response.isOk()) return;

//            IntervalUtil.updateAdDate();
            adInfoList = (AdInfoList) response.getBaseData();
            if (adInfoList == null || adInfoList.getAdInfoList().size() <= 0)
                return;
            createAdMgr(adInfoList.getAdInfoList());
        });
    }

    public void createAdMgr(List<AdInfo> advList) {
        // 创建广告活动管理对象
        AdManager adManager = new AdManager(AppTinkerLike.getActivity(), advList);
        adManager.setOverScreen(true)
                .setPageTransformer(new DepthPageTransformer())
                .setBackViewColor(Color.parseColor("#AA333333"))
                .setOnImageClickListener((view, advInfo) -> skipLogic(AppTinkerLike.getActivity(), advInfo.getUrl()));
        // 执行弹窗的显示操作
        adManager.showAdDialog(AdConstant.ANIM_DOWN_TO_UP);
    }


    public void skipLogic(Activity activity, String uri) {
        try {
            if (TextUtils.isEmpty(uri)) return;
            if (TextUtils.equals(uri, "APP:INVITE")) {
                UIShow.showInviteAct(activity);
            } else if (TextUtils.equals(uri, "NEW:SYNTH")) {
                UIShow.showSynthWawaActivity(activity);
            } else if (TextUtils.equals("Red", StringUtils.getBeforeNoFlag(uri, ":"))) {
                UIShow.showWebActivity(AppTinkerLike.getContext(), 1, StringUtils.getAfterNoFlag(uri, ":") + "?uid=" +
                        ModuleMgr.getCenterMgr().getMyInfo().getUid());
            } else if (TextUtils.equals(uri, "APP:PAY")) {
                PickerDialogUtil.showPayDialog((FragmentActivity) activity);
            } else if (TextUtils.equals("BROWSER", StringUtils.getBeforeNoFlag(uri, ":"))) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(StringUtils.getAfterNoFlag(uri, ":") + "?uid=" +
                        ModuleMgr.getCenterMgr().getMyInfo().getUid()));
                activity.startActivity(intent);
            } else {
                UIShow.showWebActivity(AppTinkerLike.getContext(), 1, uri + "?uid=" +
                        ModuleMgr.getCenterMgr().getMyInfo().getUid());
            }
//                        UIShow.showWebActivity(AppTinkerLike.getContext(), 4, "http://wawa.nicechat.cn/");
        } catch (Exception e) {
            PLogger.printThrowable(e);
        }
    }

    // ================================= 商城 ==================================

    /**
     * 获取商品列表
     *
     * @param page 页码，从1开始
     */
    public void reqGoodsList(int page, RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("limit", 100);
        getParam.put("offset", 100 * (page - 1)); // 查找偏移量
        ModuleMgr.getHttpMgr().reqGetAndCacheHttp(UrlParam.reqGoodsList, getParam, complete);
    }

    /**
     * 兑换商品
     */
    public void reqGoodsExchange(int gid, final int num, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "兑换中...");
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("gid", gid);
        postParam.put("num", num);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqGoodsExchange, postParam, response -> {
            if (complete != null)
                complete.onRequestComplete(response);

            LoadingDialog.closeLoadingDialog(200);
            PToast.showShort(response.getMsg());
            if (response.isOk()) {
                MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
            }
        });
    }

    /**
     * 商城： 我的订单
     */
    public void reqGoodsSendList(int page, RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("limit", 10);
        getParam.put("offset", 10 * (page - 1)); // 查找偏移量
        ModuleMgr.getHttpMgr().reqGetAndCacheHttp(UrlParam.reqGoodsSendList, getParam, complete);
    }

    /**
     * 商城： 我的订单状态更新
     */
    public void reqUpdateStatus(long orderId, int orderStatus, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "删除中...");
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("order_id", orderId);
        getParam.put("order_status", orderStatus); // 查找偏移量
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqUpdateStatus, getParam, response -> {
            if (complete != null) {
                complete.onRequestComplete(response);
            }

            LoadingDialog.closeLoadingDialog(200);
            PToast.showShort(response.getMsg());
            if (response.isOk()) {
                MsgMgr.getInstance().sendMsg(MsgType.MT_Store_Order_Refresh, null);
            }
        });
    }

    // ================================= 充值排行榜 ==================================

    /**
     * 获取奖品列表
     *
     * @param page 页码，从1开始
     */
    public void reqAwardList(int page, RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("limit", 10);
        getParam.put("offset", 10 * (page - 1)); // 查找偏移量
        getParam.put("order_status", "1"); // 查找订单状态
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqAwardList, getParam, complete);
    }

    /**
     * 获取奖品发货列表
     *
     * @param page 页码，从1开始
     */
    public void reqAwardOrderList(int page, RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("limit", 10);
        getParam.put("offset", 10 * (page - 1)); // 查找偏移量
        getParam.put("order_status", "2,3,4,5,6,7,8,9"); // 查找订单状态
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqAwardList, getParam, complete);
    }

    /**
     * 实体奖品： 奖品发货
     */
    public void reqAwardSend(String ids, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "兑换中...");
        ModuleMgr.getHttpMgr().reqJsonNoCacheHttp(UrlParam.reqAwardSend, ids, response -> {
            if (complete != null) {
                complete.onRequestComplete(response);
            }
            LoadingDialog.closeLoadingDialog(200);
            PToast.showShort(response.getMsg());
        });
    }

    /**
     * 虚拟奖品： 奖品发货
     */
    public void reqAwardRechange(long id, String acount, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "兑换中...");
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("activity_id", id);
        postParam.put("target", acount);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqAwardRechange, postParam, response -> {
            if (complete != null) {
                complete.onRequestComplete(response);
            }
            LoadingDialog.closeLoadingDialog(200);
            PToast.showShort(response.getMsg());
            if (response.isOk()) {
                MsgMgr.getInstance().sendMsg(MsgType.MT_Prize_Refresh, null);
            }
        });
    }

    /**
     * 奖品： 我的订单状态更新
     */
    public void reqAwardUpdate(long orderId, int orderStatus, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "删除中...");
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("order_id", orderId);
        getParam.put("order_status", orderStatus);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqAwardUpdate, getParam, response -> {
            if (complete != null) {
                complete.onRequestComplete(response);
            }

            LoadingDialog.closeLoadingDialog(200);
            PToast.showShort(response.getMsg());
            if (response.isOk()) {
                MsgMgr.getInstance().sendMsg(MsgType.MT_Prize_Order_Refresh, null);
            }
        });
    }

    /**
     * 更新娃娃机房间聊天消息到服务器
     *
     * @param rid 娃娃机id
     */
    public void reqUpdateMsg(long rid, String msgJson) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("Id", rid);
        postParam.put("MsgJson", msgJson);
        ModuleMgr.getHttpMgr().reqJsonNoCacheHttp(UrlParam.reqUpdateMsg, postParam, null);
    }

    /**
     * 获取娃娃机房间聊天消息
     */
    public void reqGetMsg(long rid, final RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("rid", rid);
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqGetMsg, getParam, response -> {
            if (complete != null) {
                complete.onRequestComplete(response);
            }
        });
    }

    /**
     * 获取是否是首抓
     */
    public void reqGameFirst(final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqGameFirst, new HashMap<>(), response -> {
            if (complete != null) {
                complete.onRequestComplete(response);
            }
        });
    }

    // =================================== 优惠券相关 ================================

    /**
     * 获取用户优惠券列表: 全部
     */
    public void reqUserCoupon(RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqUserCoupon, new HashMap<>(), complete);
    }

    /**
     * 获取用户优惠券列表: 充值时可用券
     *
     * @param productId 充值列表中的条目id， 返回该id条目可用优惠券
     */
    public void reqUserCoupon(int productId, RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("product_id", productId);
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqUserCoupon, getParam, complete);
    }

    // ==================================== 娃娃合成 ============================

    /**
     * 获取我的可合成娃娃列表
     */
    public void reqSynthWawa(RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("status", WawaConstant.WAWA_NO_SEND);
        postParam.put("limit", 500);
        postParam.put("offset", 0); // 查找偏移量
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqMyWawa, postParam, complete);
    }

    /**
     * 合成娃娃
     *
     * @param ids 抓中娃娃的游戏记录ID
     */
    public void reqComposeWawa(String ids, final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqJsonNoCacheHttp(UrlParam.reqComposeWawa, ids, response -> {
            if (complete != null) {
                complete.onRequestComplete(response);
            }
        });
    }

    /**
     * 首充开启宝箱
     */
    public void reqLuckWawa(final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqLuckWawa, new HashMap<>(), response -> {
            if (complete != null) {
                complete.onRequestComplete(response);
            }
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
            }
        });
    }

    /**
     * 兑换娃娃碎片
     */
    public void reqExchangeMaterial(int wwId, int wwNum, final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "兑换中请稍后");
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("ww_id", wwId);
        postParam.put("ww_num", wwNum);
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqExchangeMaterial, postParam, response -> {
            LoadingDialog.closeLoadingDialog(200);
            if (complete != null) {
                complete.onRequestComplete(response);
            }
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
                return;
            }
            MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
            PickerDialogUtil.showMaterialExchangeDialog((FragmentActivity) AppTinkerLike.getActivity());
        });
    }

    /**
     * 首充7天领币
     */
    public void reqRewardWawaCoin(final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "领取中...");
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqRewardWawaCoin, new HashMap<>(), response -> {
            LoadingDialog.closeLoadingDialog(200);
            if (complete != null) {
                complete.onRequestComplete(response);
            }
            MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);

            if (response.isOk()) {
                JSONObject jsonObject = response.getResponseObject();
                int awardCoin = jsonObject.optInt("FisrtRechargeWwCoinZs");
                PickerDialogUtil.showPayGetCoinDialog((FragmentActivity) AppTinkerLike.getActivity(), awardCoin);
                return;
            }
            PToast.showShort(response.getMsg());
        });
    }

    /**
     * 每日礼包：领取接口
     */
    public void reqRewardDaily(final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "领取中...");
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqRewardDaily, new HashMap<>(), response -> {
            LoadingDialog.closeLoadingDialog(200);
            if (complete != null)
                complete.onRequestComplete(response);

            if (!response.isOk())
                PToast.showShort(response.getMsg());
        });
    }

    /**
     * 每日礼包：分享到账接口
     */
    public void reqGiveRewardDaily(final RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqGiveRewardDaily, new HashMap<>(), response -> {
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    /**
     * 邀请页数据
     */
    public void reqInvitePage(RequestComplete complete) {
        ModuleMgr.getHttpMgr().reqGetAndCacheHttp(UrlParam.reqInvitePage, new HashMap<>(), complete);
    }

    /**
     * 邀请页开宝箱
     */
    public void reqInviteBox(final RequestComplete complete) {
        LoadingDialog.show(AppTinkerLike.getActivity(), "请稍候...");
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqInviteBox, new HashMap<>(), response -> {
            LoadingDialog.closeLoadingDialog(200);
            if (complete != null)
                complete.onRequestComplete(response);
        });
    }

    // =================== 问题反馈 ====================

    /**
     * 常见问题列表
     */
    public void reqQuestionList(RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("limit", 1000);
        getParam.put("offset", 0); // 查找偏移量
        ModuleMgr.getHttpMgr().reqGetAndCacheHttp(UrlParam.reqQuestionList, getParam, complete);
    }

    /**
     * 反馈问题
     */
    public void reqQuestionAdd(String phone, String content, String imageJson, RequestComplete complete) {
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("Telphone", phone);
        jsonMap.put("Content", content);
        jsonMap.put("Os", "android");
        jsonMap.put("ImageJson", imageJson);
        ModuleMgr.getHttpMgr().reqJsonNoCacheHttp(UrlParam.reqQuestionAdd, jsonMap, complete);
    }

    /**
     * 我的问题集
     */
    public void reqMyQuestion(RequestComplete complete) {
        Map<String, Object> getParam = new HashMap<>();
        getParam.put("limit", 1000);
        getParam.put("offset", 0); // 查找偏移量
        ModuleMgr.getHttpMgr().reqGetAndCacheHttp(UrlParam.reqMyQuestion, getParam, complete);
    }

    /**
     * 更改问题状态
     */
    public void reqQuestionStatus(long feed_id, RequestComplete complete) {
        Map<String, Object> postParam = new HashMap<>();
        postParam.put("feed_id", feed_id);
        postParam.put("status", 1); // 查找偏移量
        ModuleMgr.getHttpMgr().reqPostNoCacheHttp(UrlParam.reqQuestionStatus, postParam, complete);
    }

    /**
     * 遇见她： 下载奖励
     */
    public void reqRewardDownload() {
        ModuleMgr.getHttpMgr().reqGetNoCacheHttp(UrlParam.reqRewardDownload, null, null);
    }
}
